package com.societyGames.flashForms
{
	import com.societyGames.system.EventObject;
	
	import flash.events.Event;

	//Container that detects when a child class changes size because of one size change changing the other.
	public class ProportionContainer extends SingleChildContainer implements IChangeSize
	{	
		private var _desiredWidth:Number;
		private var _desiredHeight:Number;
		private var _sizeChangedEventObject:EventObject; //Event
		
		public function ProportionContainer()
		{
			this._sizeChangedEventObject = new EventObject(this, "SIZE_CHANGED");			
		}
		
		//IChangeSize -->
		public function get sizeChangedEventObject():EventObject
		{
			return this._sizeChangedEventObject;			
		}
		//<-- IChangeSize
		
		public override function set width(value:Number):void
		{	
			if (value != this._desiredWidth)
			{
				this._desiredWidth = value;
				if (this._child != null && this._child.width != this._desiredWidth)
				{
					var oldHeight:Number = this._child.height;
					this._child.width = this._desiredWidth;
					if (oldHeight != this._child.height)
					{
						this._sizeChangedEventObject.dispatchEvent();
					}
				}
			}
		}
		
		public override function set height(value:Number):void
		{	
			if (value != this._desiredHeight)
			{
				this._desiredHeight = value;
				if (this._child != null && this._child.height != this._desiredHeight)
				{
					var oldWidth:Number = this._child.width;
					this._child.height = this._desiredHeight;
					if (oldWidth != this._child.width)
					{
						this._sizeChangedEventObject.dispatchEvent();
					}
				}
			}
		}
		
		protected override function childSizeChangedHandler(event:Event):void
		{
			this._sizeChangedEventObject.dispatchEvent();
		}
	}
}